package com.woniuxy.factory.service.imp;


import com.woniuxy.factory.entity.Client;
import com.woniuxy.factory.entity.Order;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.mapper.ClientMapper;
import com.woniuxy.factory.mapper.OrderMapper;
import com.woniuxy.factory.service.OrderService;
import com.woniuxy.factory.util.AuditType;
import com.woniuxy.factory.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImp implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    ClientMapper clientMapper;
    //查询订单
    @Override
    public ResponseResult<List<Order>> getOrderAll(int page, int limit) {
        ResponseResult<List<Order>> responseReslt= new ResponseResult<List<Order>>();
        int count = orderMapper.getCount();
        int home=(page-1)*limit;
        List<Order> orders= orderMapper.getOrders(home,limit);
        responseReslt.setCount(count);
        responseReslt.setData(orders);
        responseReslt.setCode(AuditType.No_Approval);

        return responseReslt;
    }
    //新增订单
    @Override
    public ResponseResult addOrder(Integer client, Date date, String remark, HttpServletRequest request) {
        ResponseResult responseResult = new ResponseResult();
        //判断客户是否存在
        Client client1 = new Client();
        client1.setCliId(client);
        List<Client> clients = clientMapper.selectClients(client1);
        if (clients.size()==0){
            responseResult.setMsg("客户信息不存在");
            return responseResult;
        }
        //生成订单编号
        String uuid = UuidUtil.get16UUID();
        //获取当前账户的信息
        Order order = new Order();
        order.setOrdId(uuid);
        order.setOrderDate(new Date());
        order.setOrderduration(date);
        order.setClient(client1);
        order.setRemark(remark);
        //定义订单状态
        order.setOrderstate("未完成");
        //新增订单
        try {
            orderMapper.insertOrder(order);
        }catch (Exception e){
            responseResult.setMsg("系统维护中");
            return responseResult;
        }
        responseResult.setMsg("新增成功");
        return responseResult;
    }
}
